import { Message } from 'element-ui'
export default ({ $axios, store, router }) => {
    $axios.defaults.timeout = 5000;
    // $axios.defaults.baseURL = '/api'
    $axios.onRequest((config) => {
        const token = store.state.userStore?.userInfo?.token || ''
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config
    })
    $axios.onResponse((response) => {

        return response
    }, (error) => {
        // 401 token 失效处理
        if (error.response.status === 401) {
            store.commit('userStore/clearUserInfo')
            Message.warning('登录已失效，请重新登录')
            router.push('/login')
        }
    })
}